package com.hadoop.mr.join;

import com.hadoop.mr.io.CombineValues;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BooleanWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

/**
 * <p></p>
 *
 * @author Andy
 * @date 2017/10/21
 */
public class MapReduceJoin1 {

    static class HadoopMap extends Mapper<LongWritable, Text, Text, CombineValues> {
        private static final String PREFIX_NAME = "input1.txt";
        private CombineValues outVal = new CombineValues();
        private Text outKey = new Text();
        private BooleanWritable flag = new BooleanWritable();
        private String[] keyValue = null;


        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //首先拿到文件名来进行分组
            String fileName = ((FileSplit)context.getInputSplit()).getPath().getName();
            if (StringUtils.equalsIgnoreCase(PREFIX_NAME, fileName)) {
                flag.set(true);
            } else {
                flag.set(false);
            }

            keyValue = value.toString().split(",",2);
            outVal.set(keyValue[0],flag,keyValue[1]);
            outKey.set(keyValue[0]);
            context.write(outKey, outVal);
        }
    }

    static class HadoopReduce extends Reducer<Text, CombineValues, Text, Text> {
        private Text outVal = new Text();
        private Text left = new Text();
        private Text right = new Text();

        @Override
        protected void reduce(Text key, Iterable<CombineValues> values, Context context) throws IOException, InterruptedException {
            for (CombineValues cv : values) {
                if (cv.getFlag().get()) {
                    left.set(cv.getSecondPart());
                } else {
                    right.set(cv.getSecondPart());
                }
            }

            outVal.set(left + "," + right);
            context.write(key, outVal);
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(MapReduceJoin1.class);
        job.setMapperClass(HadoopMap.class);
        job.setReducerClass(HadoopReduce.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(CombineValues.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        Path outPath = new Path(args[1]);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job,outPath);
        outPath.getFileSystem(conf).delete(outPath, true);

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
